<?php
/**
 * Created by PhpStorm.
 * User: Kevin
 * Date: 2018/11/29
 * Time: 16:10
 */
namespace app\admin\controller;
use think\Db;
class Info extends Base
{
    /*
     * 古建资料列表
     */
    public function guIndex(){
        if(request()->isAjax ()){
            extract(input());
            $map = [];
            if(isset($key)&&$key!="")
            {
                $map['build_name'] = ['like',"%" . $key . "%"];
            }
            $Nowpage = input('page') ? input('page'):1;
            $limits = input("limit")?input("limit"):10;// 获取总条数;
            $count = Db::name('build_info')->where($map)->count();
            $lists = Db::name('build_info')
                ->field(['build_id','build_art','build_type','build_tag','build_abstract',"if(build_thumb!='',concat('".http_type ()."',build_thumb),build_thumb)"=>'build_thumb',"if(build_music!='',concat('".http_type ()."',build_music),build_music)"=>'build_music','build_name','build_intro','build_create_time','build_update_time'])
                ->where($map)
                ->page($Nowpage,$limits)
                ->order('build_create_time desc')
                ->select();
            return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$lists]);
        }
        return $this->fetch('info/gu_index');
    }

    /*
     * 添加古建资料
     */
    public function guAdd(){
        if(request()->isPost ()){
            extract(input());
            $time = date('Y-m-d H:i:s');
            $param = [
                'build_art'=>$build_art,
                'build_tag'=>$build_tag,
                'build_type'=>$build_type,
                'build_abstract'=>$build_abstract,
                'build_thumb'=>$build_thumb,
                'build_music'=>$build_music,
                'build_music_name'=>$music_name,
                'build_name'=>$build_name,
                'build_intro'=>$build_intro,
                'build_create_time'=>$time,
                'build_update_time'=>$time,
                'build_zip'=>$build_zip
            ];
            $res = Db::name('build_info')->insert($param);
            if($res){
                return json(['code'=>200,'msg'=>'添加成功']);
            }else{
                return json(['code'=>100,'msg'=>'添加失败']);
            }
        }
        return $this->fetch('info/gu_add');
    }

    /*
     * 编辑古建资料
     */
    public function guEdit(){
        if(request()->isPost ()){
            extract(input());
            $param = [
                'build_art'=>$build_art,
                'build_tag'=>$build_tag,
                'build_type'=>$build_type,
                'build_abstract'=>$build_abstract,
                'build_thumb'=>$build_thumb,
                'build_music'=>$build_music,
                'build_music_name'=>$music_name,
                'build_name'=>$build_name,
                'build_intro'=>$build_intro,
                'build_update_time'=>date('Y-m-d H:i:s')
            ];
            $res = Db::name('build_info')->where(['build_id'=>$build_id])->update($param);
            if($res){
                return json(['code'=>200,'msg'=>'修改成功']);
            }else{
                return json(['code'=>100,'msg'=>'修改失败']);
            }
        }
        $data = Db::name('build_info')->find(input('id'));
        return $this->fetch('info/gu_edit',['data'=>$data]);
    }

    /*
     * 删除古建资料
     */
    public function guDel(){
        $res = Db::name('build_info')->delete(input('id'));
        if($res){
            return json(['code'=>200,'msg'=>'删除成功']);
        }else{
            return json(['code'=>100,'msg'=>'删除失败']);
        }
    }

    public function excelInfo(){
        extract(input());
        if($ids =="" && $key == "" ){
            $data = Db::name('build_info')
                ->field(['build_id','build_art','build_type','build_tag','build_abstract',"if(build_thumb!='',concat('".http_type ()."',build_thumb),build_thumb)"=>'build_thumb',"if(build_music!='',concat('".http_type ()."',build_music),build_music)"=>'build_music','build_name','build_intro','build_create_time','build_update_time'])
                ->select();
        }
        if($ids != ""){
            $ids = trim($ids,',');
            $ids = explode(',',$ids);
            $data = Db::name('build_info')
                ->field(['build_id','build_art','build_type','build_tag','build_abstract',"if(build_thumb!='',concat('".http_type ()."',build_thumb),build_thumb)"=>'build_thumb',"if(build_music!='',concat('".http_type ()."',build_music),build_music)"=>'build_music','build_name','build_intro','build_create_time','build_update_time'])
                ->where('build_id','in',$ids)
                ->select();
        }else{
            $map = [];
            if($key!="")
            {
                $map['build_name'] = ['like',"%" . $key . "%"];
            }
            $data = Db::name('build_info')
                ->where($map)
                ->field(['build_id','build_art','build_type','build_tag','build_abstract',"if(build_thumb!='',concat('".http_type ()."',build_thumb),build_thumb)"=>'build_thumb',"if(build_music!='',concat('".http_type ()."',build_music),build_music)"=>'build_music','build_name','build_intro','build_create_time','build_update_time'])
                ->select();
        }
        $cellname = [
            ['build_name','古建名称',20,'LEFT'],
            ['build_thumb','缩略图',20,'LEFT'],
            ['build_tag','年代',15,'LEFT'],
            ['build_art','工艺',15,'LEFT'],
            ['build_type','文件类型',15,'LEFT'],
            ['build_music','音频',15,'LEFT'],
            ['build_abstract','摘要',30,'LEFT']
        ];
        $res = exportExcel('古建资料','admin',$cellname,$data);
        return json($res);
    }
}